package com.req.vmsg.processor;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletResponse;

import com.req.vmsg.util.TxtWebConstants;

public class VMsgResponder {
	private static final Logger log = Logger.getLogger(VMsgResponder.class.getName());
	public void relayVMsg(String mobile,String message){
		try{
			if(mobile!=null && message!=null &&
					!mobile.isEmpty() && !message.isEmpty()){
				
				String encodedPayLoadMessage = constructVMsgPayLoad(mobile,message);
				
				//Open an HTTP connection to the txtWeb PUSH API
				URLConnection conn = new URL(TxtWebConstants.TxtWebServiceAppConstants.TXTWEB_PUSH_SERVICE_ENDPOINT_URL).openConnection();
				
				//Ready to post the parameters to the API
				conn.setDoOutput(true);
				OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
				wr.write(encodedPayLoadMessage);
				wr.flush();
				log.info("Done invoking the Push API.. going to read the response!!");
				//optionally you can check for the response that was received from the API
				//Refer to the API reference sheet for more info about the response codes received
				BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
				String line = br.readLine();
				while(line !=null){
					log.info(line);
					line = br.readLine();
				}
				br.close();
			}
		}
		catch(UnsupportedEncodingException e){e.printStackTrace();} 
		catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private String constructVMsgPayLoad(String mobile, String message) {
		String payLoadStr = null;
	
		//Encode all the parameters to UTF-8 format
		StringBuffer payLoad = new StringBuffer("");
		payLoad.append(encode("txtweb-mobile"));
		payLoad.append("=");
		payLoad.append(encode(mobile));
		payLoad.append("&");
		payLoad.append(encode("txtweb-pubkey"));
		payLoad.append("=");
		payLoad.append(TxtWebConstants.TxtWebServiceAppConstants.TXTWEB_PUBLISHER_KEY);
		payLoad.append("&");
		payLoad.append(encode("txtweb-message"));
		payLoad.append("=");
		payLoad.append(encode("<HTML><head><meta name=\"txtweb-appkey\" content=\"" + TxtWebConstants.TxtWebServiceAppConstants.TXTWEB_APPLICATION_KEY + "\"></head><BODY>"));
		payLoad.append(encode(message));
		payLoad.append(encode("</BODY></HTML>"));
		
		payLoadStr = payLoad.toString();
		
		log.info(payLoadStr);
	
		return payLoadStr;
	}
	
	private String encode(String plainString)
	{
		String encodedString = null;
		
		try 
		{
			encodedString = URLEncoder.encode(plainString , "UTF-8");
		} 
		catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		return encodedString;
	}
}
